Fix #624: preserve duplicate output column names in query results#722
Closed
RamiNoodle733 wants to merge 1 commit into
Closed
Fix #624: preserve duplicate output column names in query results#722RamiNoodle733 wants to merge 1 commit into
RamiNoodle733 wants to merge 1 commit into
Conversation
simonw
added a commit
that referenced
this pull request
Jul 6, 2026
Queries returning duplicate column names - e.g. joins between tables sharing column names - silently lost values because rows were built with dict(zip(keys, row)), where the last duplicate wins. Later occurrences are now renamed with a numeric suffix: id, id becomes id, id_2 - skipping any suffix that would collide with a real column in the same query. The new utils.dedupe_keys() helper transforms the key list once per query, so the per-row dict construction is unchanged and there is no measurable performance impact. Applied in Database.query() (including the PRAGMA and RETURNING paths), Table.rows_where(), Table.search() and the CLI's JSON output. CSV, TSV and table output keep the original duplicate headers. Refs #722 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Owner
|
Fixed here instead: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes:
Summary
When a query returns duplicate column names (for example joining two tables with the same field names),
Database.query()and related helpers useddict(zip(...)), which silently overwrote earlier values.This change preserves all values by suffixing duplicate keys as
_2,_3, etc.Changes
_row_to_dict()helper to safely map row values to unique dictionary keysDatabase.query(),Queryable.rows_where(), andTable.search()to use itValidation
pytest -q tests/test_query.py tests/test_rows.pyruff check sqlite_utils/db.py tests/test_query.py